package com.utonw.carloan.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;

import com.utonw.carloan.app.MyApplication;


/**
 * Created by Administrator on 2017/1/30.
 */

public class UIUtils {
    private static Toast toast;

    public static Context getContext() {
        return MyApplication.getContext();

    }

    public static Handler getHandler() {

        return MyApplication.getHandler();

    }

    public static int getMainThreadId() {
        return MyApplication.getMyTid();

    }

    public static String getString(int id) {

        //获取字符串
        return getContext().getResources().getString(id);


    }

    public static String[] getStringArray(int id) {
        //获取字符串数组
        return getContext().getResources().getStringArray(id);

    }

    //获取图片
    public static Drawable getDrawable(int id) {

        return getContext().getResources().getDrawable(id);

    }

    //获取颜色
    public static int getColor(int id) {

        return getContext().getResources().getColor(id);
    }

    //获取尺寸
    public static int getDimen(int id) {
        return getContext().getResources().getDimensionPixelSize(id);

    }


    //dip和px转换

    public static int dip2px(float dip) {

        //获取设备密度
        float density = getContext().getResources().getDisplayMetrics().density;
        return (int) (dip * density + 0.5f);

    }


    public static float px2dip(int px) {

        float density = getContext().getResources().getDisplayMetrics().density;
        return px / density;

    }

    public static int px2sp(Context context, float pxValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (pxValue / fontScale + 0.5f);
    }

    //加载布局文件
    public static View inflate(int id) {
        return LayoutInflater.from(getContext()).inflate(id, null);
    }


    //判断是否在主线程
    public static boolean isRunOnUiThread() {
        //获取当前线程id，如果当前线程id和主线程id相同，那么当前就是主线程
        int myTid = android.os.Process.myTid();
        return myTid == getMainThreadId();

    }

    public static void runOnUiThread(Runnable runnable) {
        if (isRunOnUiThread()) {
            //已经是主线程
            runnable.run();
        } else {
            //如果是子线程，那么就让Handler运行在主线程
            getHandler().post(runnable);

        }


    }

    public static void showShortToast(CharSequence sequence) {

        if (toast == null) {
            toast = Toast.makeText(MyApplication.getContext(), sequence, Toast.LENGTH_SHORT);

        } else {
            toast.setText(sequence);
        }
        toast.show();

    }

    public static void hideSoftKeyboard(Context context) {
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            imm.hideSoftInputFromWindow(((Activity)context).getWindow().getDecorView().getWindowToken(), 0);
        }

    }


}
